/******************************************************************************
*@file  : app.c
*@brief : UART Demos 
******************************************************************************/
#include "app.h"

uint8_t TxBuffer[512];
uint8_t RxBuffer[512];

const char *prompt_str = "Please enter any String/Data";

UART_HandleTypeDef  UART1_Handle;
UART_HandleTypeDef  UART2_Handle;

volatile uint32_t TxCpltStatus = false, TxCpltStatus2 = false, TxCpltStatus3 = false;

volatile uint32_t RxCpltStatus = false, RxCpltStatus2 = false, RxCpltStatus3 = false;

/******************************************************************************
*@brief : Sample code for initialising UART1 
*@param : none
*@return: none
******************************************************************************/
void UART1_Init(void) 
{
    UART1_Handle.Instance        = UART1;    
    UART1_Handle.Init.BaudRate   = 115200; 
    UART1_Handle.Init.WordLength = UART_WORDLENGTH_8B;
    UART1_Handle.Init.StopBits   = UART_STOPBITS_1;
    UART1_Handle.Init.Parity     = UART_PARITY_NONE;
    UART1_Handle.Init.Mode       = UART_MODE_TX_RX;
    UART1_Handle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
    
    HAL_UART_Init(&UART1_Handle);  
}

/******************************************************************************
*@brief : Sample code for initialising UART2 
*@param : none
*@return: none
******************************************************************************/
void UART2_Init(void) 
{
    UART2_Handle.Instance        = UART2;    
    UART2_Handle.Init.BaudRate   = 115200; 
    UART2_Handle.Init.WordLength = UART_WORDLENGTH_8B;
    UART2_Handle.Init.StopBits   = UART_STOPBITS_1;
    UART2_Handle.Init.Parity     = UART_PARITY_NONE;
    UART2_Handle.Init.Mode       = UART_MODE_TX_RX;
    UART2_Handle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
    
    HAL_UART_Init(&UART2_Handle);  
}


/******************************************************************************
*@brief : UART1 IT tx completed callback
*@param : none
*@return: none
******************************************************************************/
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
    if(huart->Instance ==UART1)  // Transfor Data By UART1
    {
		TxCpltStatus= true;
    }
    else if(huart->Instance ==UART2) //Transfor Data By UART2 
	{
		TxCpltStatus2 =true;
	}
}


void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{	
    if(huart->Instance == UART1)  // receive data by UART1
    {
		RxCpltStatus = true;
    }
    else if(huart->Instance == UART2) //receive data by UART2  
	{
		RxCpltStatus2 = true;
	}
}

void HAL_UART_IdleCallback(UART_HandleTypeDef *huart)
{
#if 1
    /* Finish receiving directly */
    HAL_UART_RxCpltCallback(huart);
#else
    /* Finish receiving when all of data have been received */
    if(huart->RxCount < huart->RxSize)
    {
        __HAL_UART_Resume_Receive_IT(huart);
    }
    else
    {
        HAL_UART_RxCpltCallback(huart);
    }
#endif 
}

void APP_Test(enum_TEST_MODE_t fe_Mode)
{
	uint8_t ret;
    
    switch (fe_Mode)
    {
        /* Loop mode*/
        case TEST_UART1_LOOP: 
        {
            printfS("----------UART1 LOOP Demo ----------\r\n");
            printfS("%s to [UART1]\r\n\r\n", prompt_str);
            for(;;)
            {
                //HAL_UART_Receive(&UART1_Handle, RxBuffer, sizeof(RxBuffer), 10);
                HAL_UART_Receive_To_IDLEorBCNT(&UART1_Handle, RxBuffer, sizeof(RxBuffer), RECEIVE_TOIDLE, 115200);
                if (UART1_Handle.RxCount)
                {
                    HAL_UART_Transmit(&UART1_Handle, RxBuffer, UART1_Handle.RxCount, HAL_MAX_DELAY);  

                    memset(RxBuffer, 0, sizeof(RxBuffer)); 
                }
            }
        }break;

        /* Interrupt mode */
        case TEST_UART1_IT: 
        {
            printfS("----------UART1 IT Demo ----------\r\n");
            printfS("%s to [UART1]\r\n\r\n", prompt_str);
            while(1)
            {
                /* receive variable length data by intrrupt method */
                HAL_UART_Receive_IT(&UART1_Handle, RxBuffer, sizeof(RxBuffer)); 
						
                while (!RxCpltStatus);
                
                RxCpltStatus=false;
                
                memcpy(TxBuffer, RxBuffer, UART1_Handle.RxCount);

                HAL_UART_Transmit_IT(&UART1_Handle, TxBuffer, UART1_Handle.RxCount); 
                
                while (!TxCpltStatus);  
            
                TxCpltStatus =false;
                
                /* wait tx xfer completed */
                while ((UART1_Handle.Instance->FR & UART_FR_BUSY));
            
            }
        }break;
     
		case TEST_UART2_IT:
		{
            printfS("----------UART2 Demo ----------\r\n");
            printfS("%s to [UART2]\r\n\r\n", prompt_str);           
			while(1)
			{
				HAL_UART_Receive_IT(&UART2_Handle, RxBuffer, sizeof(RxBuffer));    // length should <= sizeof(RxBuffer)   
						
				while(1)  
				{
					while (!RxCpltStatus2);
				
					RxCpltStatus2=false;
				
					memcpy(TxBuffer, RxBuffer, UART2_Handle.RxCount);

					HAL_UART_Transmit_IT(&UART2_Handle, TxBuffer, UART2_Handle.RxCount); 
				
					while (!TxCpltStatus2);  
				
					TxCpltStatus2 =false;
                    
                    /* wait tx xfer completed */
                    while ((UART2_Handle.Instance->FR & UART_FR_BUSY));

					break;		   
				}
			}
		}break;
        
        case TEST_UART2_LOOP: 
        {
            printfS("----------UART2 LOOP Demo ----------\r\n");
            printfS("%s to [UART2]\r\n\r\n", prompt_str);
            for(;;)
            {
                HAL_UART_Receive_To_IDLEorBCNT(&UART2_Handle, RxBuffer, sizeof(RxBuffer), RECEIVE_TOIDLE, 115200);
                if (UART2_Handle.RxCount)
                {
                    HAL_UART_Transmit(&UART2_Handle, RxBuffer, UART2_Handle.RxCount, HAL_MAX_DELAY);  

                    memset(RxBuffer, 0, sizeof(RxBuffer)); 
                }
            }
        }break;
        
        default: break; 
    }
}
